//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
//==
//==
//==
//==================================================================

#include "stdafx.h"
#include "m5d_mesh.h"
#include "m5d_utils_ext.h"
#include "m5d_meshb_clusters.h"

//==================================================================
using namespace M5D;

//==================================================================
#ifdef _DEBUG
static void check_valid_bnlist( const u_char *lstp, int n )
{
	bool marked = false;

	int	i = 0;

	for (; i < n; ++i)
		if ( lstp[i] == 255 )
		{
			PASSERT( marked == false );
			marked = true;
		}

	for (; i < 4; ++i)
		if ( lstp[i] != 255 )
		{
			PASSERT( 0 );
		}
	
}
#endif

//==================================================================
static void init_bvgroup( verts_bones_cluster_t *vclusterp, const u_char *bnidxp, int n )
{
int		i;

	vclusterp->bonelist.mask = 0xffffffff;
	vclusterp->n_verts = 0;
	vclusterp->bones_n = 0;
	for (i=0; i < n; ++i)
		if ( (vclusterp->bonelist.array[i] = bnidxp[i]) != 255 )
			vclusterp->bones_n += 1;

	// starts considering the group rigid, this will be modified if, when parsing vertices,
	// the weight will be different from 1.0f
	if ( vclusterp->bones_n == 1 )
		vclusterp->flags = M5D_BONEMASK_GROUP_FLG_RIGID;
	else
		vclusterp->flags = 0;
}

//==================================================================
static int cmp_bnlist( u_char *ap, u_char *bp, int n )
{
int	diff = 0;

	while ( (--n >= 0) && !(diff = *ap++ - *bp++) )
		;

	return diff;
}
